package com.lc.cehui.ui.activity.mine;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.bar.TitleBar;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.cehui.R;
import com.lc.cehui.app.AppActivity;
import com.lc.cehui.http.request.StoreCouponApi;
import com.lc.cehui.http.response.StoreCouponBean;
import com.lc.cehui.other.IntentKey;
import com.lc.cehui.other.KeyboardWatcher;
import com.lc.cehui.util.SPUtil;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CouponManagementActivity extends AppActivity implements KeyboardWatcher.SoftKeyboardStateListener,
        TextView.OnEditorActionListener{
    @BindView(R.id.titleBar)
    TitleBar titleBar;
    @BindView(R.id.tv_coupon)
    TextView tvCoupon;
    @BindView(R.id.et_coupon)
    EditText etCoupon;
    @BindView(R.id.rv_coupon)
    RecyclerView rvCoupon;
    private BaseQuickAdapter mAdapter;
    private List<StoreCouponBean.DataBean> dataList=new ArrayList<>();
    @Override
    protected int getLayoutId() {
        return R.layout.activity_couponmanagement;
    }

    @Override
    protected void initView() {
        rvCoupon.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new BaseQuickAdapter<StoreCouponBean.DataBean, BaseViewHolder>(R.layout.item_coupon, dataList) {
            @Override
            protected void convert(@NotNull BaseViewHolder baseViewHolder, StoreCouponBean.DataBean bean) {
                baseViewHolder.setText(R.id.tv_money,bean.getRed_price()+"");
                baseViewHolder.setText(R.id.tv_manjian,"满"+bean.getShold_price()+"可用");
                baseViewHolder.setText(R.id.tv_type,bean.getName());

                baseViewHolder.setText(R.id.tv_youxiaodate,"有效期至"+bean.getEnd_time());


            }
        };
        rvCoupon.setAdapter(mAdapter);
        showEmptyView(mAdapter);
        store_coupon();
    }

    private void store_coupon() {
        EasyHttp.post(this)
                .api(new StoreCouponApi().setStore_id(SPUtil.get(this, IntentKey.USERID,0)).setKey(etCoupon.getText().toString()))
                .request(new HttpCallback<StoreCouponBean>(this) {

                    @Override
                    public void onSucceed(StoreCouponBean data) {

                        if (data.getCode().equals("200")) {
                            dataList.clear();
                            dataList.addAll(data.getData());
                            mAdapter.setNewData(dataList);
                            mAdapter.notifyDataSetChanged();
                        }

                    }
                });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.rl_seach, R.id.tv_creat, R.id.bt_submit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_seach:
                break;
            case R.id.tv_creat:
                startActivity(CreateCouponActivity.class);
                break;
            case R.id.bt_submit:
                break;
        }
    }

    @Override
    public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            // 模拟点击登录按钮


            dataList.clear();
            store_coupon();
            hideKeyboard(getCurrentFocus());
//            搜索


        }
        return  false;
    }

    @Override
    public void onSoftKeyboardOpened(int keyboardHeight) {

    }

    @Override
    public void onSoftKeyboardClosed() {

    }
}
